# from django.shortcuts import render

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import urllib


#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回
    soup = BeautifulSoup(response.text, "html5lib")

    # allNum = soup.find_all('h1',id="number-of-search-results-and-search-terms")[0].find_all('strong')[0].text.replace(',','')

    lis = soup.select("li[class='results-list__list-item media']")

    for li in lis:
        tempData = resultData()
        try:
            tempData["titleName"] = li.select(".search_result")[0].text.strip()
        except:
            pass


        try:
            tempData["titleHref"] = "https://materials.springer.com" + li.select(".search_result")[0]["href"]
        except:
            pass


        allData.append(tempData)

    return allData

def getData(keyword):

    headers = {
        'authority': 'materials.springer.com',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'referer': 'https://materials.springer.com/',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        # 'cookie': '_ga=GA1.2.2040079038.1541733459; __gads=ID=79a9252e6359b97e:T=1541733458:S=ALNI_MZUViAQgePKYodCd_8z2LCVAMmAdw; trackid=99fb742975c24d11ac5fff7d6; idp_session=sVERSION_1b97d84d6-3aab-4de1-8ac2-9cda7c9917cc; idp_session_http=hVERSION_1020fdd66-86ed-4edb-b58f-6deab1754b37; idp_marker=2f9fcaa2-5cd3-4cfe-ade0-73066d3efd5b; OptanonConsent=landingPath=https%3A%2F%2Fmaterials.springer.com%2F&datestamp=Thu+Feb+21+2019+13%3A46%3A37+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=3.6.19; _gid=GA1.2.2003081844.1550728001; _gat_UA-50822590-2=1; ki_t=1550728038585%3B1550728038585%3B1550728038585%3B1%3B1; ki_r=',
    }

    params = (
        ('searchTerm', keyword),
        ('propertyFacet', ''),
    )

    response = requests.get('https://materials.springer.com/search', headers=headers, params=params)

    allData = formData(response)

    return allData

def index(request):
    data = json.loads(request.POST.get('data', '1'))

    allData = getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")